package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.LoggingEvent;
import com.itheima.reggie.service.LoggingEventService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author : xyiaao
 * @createDate : 2022/3/2 14:07
 */
@Slf4j
@RestController
@RequestMapping("/log")
public class LogController {

    @Autowired
    private LoggingEventService loggingEventService;


    /**
     * 管理端
     * 日志分页/查询
     * @param page
     * @param pageSize
     * @param isError
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page<LoggingEvent>> page(int page, int pageSize, String isError, String beginTime, String endTime) {

        Page<LoggingEvent> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<LoggingEvent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(isError), LoggingEvent::getIsError, isError);
        queryWrapper.between(StringUtils.isNotEmpty(beginTime) && StringUtils.isNotEmpty(endTime), LoggingEvent::getCreateTime, beginTime, endTime);
        loggingEventService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }
}